﻿using System;
using System.Linq;
using TaskManager.Domain.BusinessObjects;
using TaskManager.Domain.Repositories;

namespace TaskManager.Domain.Services
{
    /// <summary>
    /// Implements validation rules for <see cref="User"/>
    /// </summary>
    internal static class UserValidationRules
    {
        /// <summary>
        /// Checks if user can be safely deleted
        /// </summary>
        /// <param name="userName">User name</param>
        /// <param name="taskRepository">Tasks repository</param>
        /// <returns></returns>
        public static Tuple<bool, string> PermitDelete(string userName, ITaskRepository taskRepository)
        {
            bool userHasNotClosedTasks = taskRepository.All
                .Where(task => (task.Owner == userName) || (task.Assignee == userName))
                .Where(task => task.State != TaskState.Finished).Any();

            if(userHasNotClosedTasks)
            {
                return new Tuple<bool, string>(false, Resource.UserValidationRules_PermitDelete_User_has_not_closed_tasks);
            }

            return new Tuple<bool, string>(true, string.Empty);
        }
    }
}